package com.feilong.commons.office.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import com.feilong.commons.core.Constants;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPRow;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 处理PDF文档 (PDF)
 * 
 * @author 金鑫 2010-2-27 下午05:14:03
 */
public class FeiLongPdf{

	/**
	 * 测试生成的pdf文件路径
	 */
	private static String	testPDFFilePath	= "E:\\test.pdf";


	/**
	 * html文件转换成pdf
	 * 
	 * @param htmlPath
	 *            需要被转换的html路径及名称
	 * @param outputFileName
	 *            输出pdf文件路径级名称
	 * @author 金鑫
	 * @version 1.0 2010-2-26 下午06:01:45
	 */
	public void htmlToPDF(String htmlPath,String outputFileName){
		Document document = new Document();
		try{
			StyleSheet styleSheet = new StyleSheet();
			styleSheet.loadTagStyle("body", "leading", "16,0");
			PdfWriter.getInstance(document, new FileOutputStream(outputFileName));
			document.open();
			ArrayList arrayList = HTMLWorker.parseToList(new FileReader(htmlPath), styleSheet);
			for (int i = 0; i < arrayList.size(); ++i){
				document.add((Element) arrayList.get(i));
			}
			document.close();
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (DocumentException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}
	}

	public void test(){
		// 创建一个Document对象
		Document document = new Document();
		try{
			// 生成名为 HelloWorld.pdf 的文档
			PdfWriter.getInstance(document, new FileOutputStream(testPDFFilePath));
			// 添加PDF文档的一些信息
			document.addTitle("Hello World example");
			document.addAuthor("Bruno Lowagie");
			document.addSubject("This example explains how to add metadata.");
			document.addKeywords("iText, Hello World, step 3, metadata");
			document.addCreator("My program using iText");
			// 打开文档，将要写入内容
			document.open();
			// 插入一个段落
			document.add(new Paragraph("Hello World!"));
		}catch (DocumentException de){
			System.err.println(de.getMessage());
		}catch (IOException ioe){
			System.err.println(ioe.getMessage());
		}
		// 关闭打开的文档
		document.close();
	}

	public void createPDF(){
		Document document = new Document(PageSize.A4);
		try{
			PdfWriter.getInstance(document, new FileOutputStream(testPDFFilePath));
			// 打开文档，将要写入内容
			document.open();
			/**
			 * 新建一个字体,iText的方法
			 * 
			 * <pre>
			 *  STSongStd-Light 是字体，
			 *  在iTextAsian.jar 中以property为后缀
			 * 	UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀
			 * 	H 代表文字版式是 横版,相应的V代表竖版
			 * </pre>
			 */
			// 标题字体
			BaseFont titleBaseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font titleFont = new Font(titleBaseFont, 18, Font.NORMAL);
			// 内容字体
			BaseFont contentBaseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font font = new Font(contentBaseFont, 9, Font.NORMAL);
			// 插入一个段落
			Paragraph titleP = new Paragraph("儿童信息 Child Information" + Constants.lineSeparator + Constants.lineSeparator, titleFont);
			titleP.setAlignment(titleP.ALIGN_CENTER);
			document.add(titleP);
			// 生成4列的表格
			PdfPTable table = new PdfPTable(4);
			table.setWidthPercentage(100);
			table.setWidthPercentage(100);
			table.addCell(new Paragraph("Children-id", font));
			PdfPCell cell = new PdfPCell(new Paragraph("09140800002", font));
			cell.setColspan(3);
			table.addCell(cell);
			// 添加第一行
			table.addCell(new Paragraph("Name(CN)", font));
			table.addCell(new Paragraph("党宁生", font));
			table.addCell(new Paragraph("Name(EN)", font));
			table.addCell(new Paragraph("DANG NING SHENG", font));
			// 添加第二行
			table.addCell(new Paragraph("Sex(CN)", font));
			table.addCell(new Paragraph("男", font));
			table.addCell(new Paragraph("Sex(EN)", font));
			table.addCell(new Paragraph("MALE", font));
			// 添加第8行
			table.addCell(new Paragraph("Note", font));
			cell = new PdfPCell(new Paragraph("儿童资料", font));
			cell.setColspan(3);
			table.addCell(cell);
			// 添加第9行
			table.addCell(new Paragraph("Pictures", font));
			Image photo = Image.getInstance("E:\\Meizu\\PICTURES\\Ofen Use\\图标\\游戏图片\\QQ农场\\白萝卜.png");
			cell = new PdfPCell(photo);
			cell.setColspan(3);
			table.addCell(cell);
			for (PdfPRow pdfPRow : (ArrayList<PdfPRow>) table.getRows()){
				for (PdfPCell cells : pdfPRow.getCells()){
					if (cells != null){
						cells.setPadding(10.0f);
					}
				}
			}
			document.add(table);
			// 换页
			document.newPage();
			// 插入图片
			document.newPage();
			// 可以是绝对路径，也可以是URL
			Image image1 = Image.getInstance("E:\\Meizu\\PICTURES\\Ofen Use\\图标\\游戏图片\\QQ农场\\金桔.png");
			// Image image = Image.getInstance(new URL(http://xxx.com/logo.jpg));
			image1.setAlignment(image1.ALIGN_CENTER);
			image1.setAbsolutePosition(0, 0);
			image1.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());
			document.add(image1);
			// 关闭打开的文档
			document.close();
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (BadElementException e){
			e.printStackTrace();
		}catch (MalformedURLException e){
			e.printStackTrace();
		}catch (DocumentException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}
	}
}
